﻿<template>
	<div class="card-fill layout-padding" v-loading="loading" element-loading-text="Loading...">
		<el-card shadow="hover" class="layout-padding-auto create-form">
			<el-form ref="formRef" :model="stateData.vmModel" label-width="120px">

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>家属信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_MemberName_FormItem"
								prop="Entity.MemberName"
								:rules="[{ required: true, message: '家属姓名为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="家属姓名" tooltip="家属的姓名" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.MemberName" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<!-- <el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Ethnicity_FormItem" prop="Entity.Ethnicity">
								<template #label>
									<WtmLabelTooltip label="民族" tooltip="家属民族" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.Ethnicity" clearable class="width100">
									<el-option key="汉族" value="汉族" label="汉族"></el-option>
									<el-option key="回族" value="回族" label="回族"></el-option>
									<el-option key="满族" value="满族" label="满族"></el-option>
									<el-option key="蒙古族" value="蒙古族" label="蒙古族"></el-option>
									<el-option key="壮族" value="壮族" label="壮族"></el-option>
									<el-option key="其他" value="其他" label="其他"></el-option
								></el-select>
							</el-form-item>
						</el-col> -->
						<!-- <el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_BirthDate_FormItem" prop="Entity.BirthDate">
								<template #label>
									<WtmLabelTooltip label="家属出生日期" tooltip="家属的出生日期" />
								</template>
								<el-date-picker v-model="stateData.vmModel.Entity.BirthDate" clearable class="width100"></el-date-picker>
							</el-form-item>
						</el-col> -->
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_WorkOrSchool_FormItem" prop="Entity.WorkOrSchool">
								<template #label>
									<WtmLabelTooltip label="工作单位/就读学校" tooltip="家属的工作单位或就读学校" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.WorkOrSchool" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<!-- <el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_JobOrGrade_FormItem" prop="Entity.JobOrGrade">
								<template #label>
									<WtmLabelTooltip label="职位/年级信息" tooltip="家属的职位或年级信息" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.JobOrGrade" clearable class="width100"></el-input>
							</el-form-item>
						</el-col> -->
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_ContactPhone_FormItem"
								prop="Entity.ContactPhone"
								:rules="[
									{ required: true, message: '家属联系电话为必填项', trigger: 'blur' },
									{ validator: validatePhone, trigger: 'blur' },
								]"
							>
								<template #label>
									<WtmLabelTooltip label="家属联系电话" tooltip="家属的联系电话" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.ContactPhone" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_RelationshipType_FormItem"
								prop="Entity.RelationshipType"
								:rules="[{ required: true, message: '亲属关系类型为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="亲属关系类型" tooltip="与员工的亲属关系类型" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.RelationshipType" clearable class="width100">
									<el-option key="父亲" value="父亲" label="父亲"></el-option>
									<el-option key="母亲" value="母亲" label="母亲"></el-option>
									<el-option key="配偶" value="配偶" label="配偶"></el-option>
									<el-option key="子女" value="子女" label="子女"></el-option>
									<el-option key="其他" value="其他" label="其他"></el-option
								></el-select>
							</el-form-item>
						</el-col>
					</el-row>
				</div>
			</el-form>
		</el-card>
	</div>
</template>

<script setup lang="ts" name="message._system.common.vm.add;false">
import ElMessage from 'element-plus/es/components/message/index';
import { reactive, ref, getCurrentInstance, onMounted, nextTick } from 'vue';
import { SysPersonFamilyApi as api } from '/@/views/person/syspersonfamily/api';
import other from '/@/utils/other';
import { ISysPersonFamilyVm } from '/@/views/person/syspersonfamily/api/interface';
import { IViewModel } from '/@/types/common';
// 使用通用的手机号验证方法
const validatePhone = other.validatePhone;

const ci = getCurrentInstance() as any;
const loading = ref(false);
// 标记PersonId是否是从外部传入的（用于控制是否禁用选择框）
const isPersonIdFromExternal = ref(false);
// 定义变量内容
const formRef = ref();
const stateData = reactive<IViewModel<ISysPersonFamilyVm>>({
	vmModel: {
		Entity: {
			PersonId: null,
			MemberName: null,
			Ethnicity: null,
			BirthDate: null,
			WorkOrSchool: null,
			JobOrGrade: null,
			ContactPhone: null,
			RelationshipType: null,
		},
	},
	AllSysPersons: [] as any[],
});

// 取消
const onClose = () => {
	closeDialog();
};

// 提交
const onSubmit = () => {
	formRef.value?.validate((valid: boolean) => {
		if (valid) {
			loading.value = true;
			api()
				.add(stateData.vmModel)
				.then(() => {
					ElMessage.success(ci.proxy.$t('message._system.common.vm.submittip'));
					emit('refresh');
					closeDialog();
				})
				.catch((error) => {
					other.setFormError(ci, error);
				})
				.finally(() => {
					loading.value = false;
				});
		}
	});
};

// 页面加载时
onMounted(() => {
	loading.value = true;
	if (ci.attrs['wtmdata']) {
		stateData.vmModel.Entity.ID = ci.attrs['wtmdata'].ID;
		// 如果传入了PersonId，设置到实体中
		if (ci.attrs['wtmdata'].PersonId) {
			stateData.vmModel.Entity.PersonId = ci.attrs['wtmdata'].PersonId;
		}
	}
	loading.value = false;
	nextTick(() => {
		setupHeaderButtons();
	});
});

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh', 'closeDialog','updateHeaderButtons']);
// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};

// 设置对话框头部按钮
const setupHeaderButtons = () => {
	const headerButtons = [
		{
			text: ci.proxy.$t('message._system.common.vm.submit'),
			type: 'primary',
			onClick: onSubmit,
			visible: true,
		},
		{
			text: ci.proxy.$t('message._system.common.vm.cancel'),
			type: 'default',
			onClick: onClose,
			visible: true,
		},
	];

	emit('updateHeaderButtons', headerButtons);
};
</script>
